# -*- encoding: utf-8 -*-
##############################################################################
#
##############################################################################

from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp
from odoo.exceptions import UserError, ValidationError
from odoo.tools import float_round


class stock_move(models.Model):
    _inherit = 'stock.move'

    feed_rate = fields.Float(u'投料浮动范围(正负%)', related='bom_line_id.feed_rate', store=True)

    @api.onchange('quantity_done')
    def onchange_quantity_done(self):
        warning = {}
        if self.product_uom_qty>0 and self.quantity_done>0:
            if abs((self.quantity_done-self.product_uom_qty)/self.product_uom_qty)>self.feed_rate:
                warning['title'] = u'警告！'
                warning['message'] = _(u'已投料数量必须在设定的允许投料范围内,上限:' + ' %s.' + u'下限:'+' %s.') % (self.product_uom_qty*(1-self.feed_rate),self.product_uom_qty*(1+self.feed_rate))
                # self.quantity_done = 0.0
                return {'warning': warning}